﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Domain.Services;
using Eacademy.Library.Actions;
using Eacademy.Library.Membership;

namespace Eacademy.Areas.Mentor.Controllers
{
    [HandleError]
    [ProfileControllerFilter]
    public class StudentsController : Controller
    {
        private const int DefaultPageSize = 10;
        private IUserRepository _userRepository;
        IUserService _userService;
        private IStudentRepository _studentRepository;
        private ISubjectRepository _subjectRepository;
        private IAssessmentRepository _assessmentRepository;
        private IMentorRepository _mentorRepository;

        CustomMembershipProvider provider = (CustomMembershipProvider)Membership.Provider;

        public StudentsController(IUserRepository userRepository, IStudentRepository studentRepository, ISubjectRepository subjectRepository, IAssessmentRepository assessmentRepository,IMentorRepository mentorRepository)
        {
            _userRepository = userRepository;
            _userService = new UserService(ModelState, studentRepository, userRepository);
            _studentRepository = studentRepository;
            _subjectRepository = subjectRepository;
            _assessmentRepository = assessmentRepository;
            _mentorRepository = mentorRepository;
        }

        //
        // GET: /Mentors/View/

        public ActionResult View(int? page, bool status = false, int id = 0)
        {
            if (id < 1)
            {
                return Redirect(Url.Action("Index", new { Area = "Mentor", Controller = "Subjects" }));
            }
            AdminSubjectsViewModel subject = _subjectRepository.GetSubjectBySubjectId(id);
            int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
            StudentListViewModel student = new StudentListViewModel();
            StudentFilterViewModel studentFilterViewModel = new StudentFilterViewModel();
            int totalcount = 0;
            List<StudentCompactViewModel> studentViewList = _studentRepository.GetStudentsBySubjectAndUserId(currentPageIndex + 1, DefaultPageSize, id, (int)provider.User.UserId, status, studentFilterViewModel, out totalcount);
            student.Status = status;
            student.SubjectName = subject.SubjectName;
            student.SubjectId = subject.SubjectId;
            student.StudentList = studentViewList.ToPagedList(currentPageIndex, DefaultPageSize, totalcount);
            return View("Index", student);
        }

        [HttpPost]
        public ActionResult Accept(int id, int subjectid)
        {
            if (_studentRepository.AcceptStudent(id, subjectid, (int)provider.User.UserId))
            {
                this.FlashDone("The student has been suuccessfully enrolled to this subject");
            }
            else
            {
                this.FlashError("There was an error enrolling the student to this subject");
            }
            //Response.TrySkipIisCustomErrors = true;
            //Response.StatusCode = (int)HttpStatusCode.BadRequest;)

            return Content("OK");
        }

        
        public ActionResult Assess(int studentid, int subjectid)
        {
            int assessmentId = 1;
            AssessmentViewModel assessmentViewList = _assessmentRepository.GetQuestionsbyAssessmentId(assessmentId);
            assessmentViewList.StudentId = studentid;
            assessmentViewList.SubjectId = subjectid;
            AdminStudentsViewModel student = _studentRepository.GetStudentByStudentId(assessmentViewList.StudentId);
            assessmentViewList.StudentName = student.FirstName + ' ' + student.LastName;
            return View("Assess",assessmentViewList);

        }

        [HttpPost]
        public ActionResult Assess(AssessmentViewModel assessmentViewModel)
        {
            int assessmentId = 1;
            assessmentViewModel.AssessmentId = assessmentId;
            AdminMentorsViewModel mentor = _mentorRepository.GetMentorByUserId((int)provider.User.UserId);
            assessmentViewModel.MentorId = mentor.MentorId;
            AdminStudentsViewModel student = _studentRepository.GetStudentByStudentId(assessmentViewModel.StudentId);
            assessmentViewModel.StudentName = student.FirstName + ' ' + student.LastName;
            AssessmentViewModel assessmentViewModels = _assessmentRepository.AddStudentRating(assessmentViewModel);
            assessmentViewModel = _assessmentRepository.GetQuestionsbyAssessmentId(assessmentId);
            return View("Assess",assessmentViewModel);
        }
    }
}
